﻿#ifndef HEADER_3AF674134B9215F
#define HEADER_3AF674134B9215F

#ifndef _DISK_INFO_SET_
#define _DISK_INFO_SET_

#include "rec_info.h"
#include "indexSet.h"
#include "STFS_com.h"

#include <vector>
#include <list>
#include <set>
#include <string>
typedef vector<DISK_INFO> VEC_DISKINFO;
typedef list<DISK_INFO>   LST_DISKINFO;

using namespace std;

class CDiskInfoSet
{
public:
	CDiskInfoSet();
	~CDiskInfoSet();
public:
	// static member variable
	static CDiskInfoSet* _Instance;
public:
	static CDiskInfoSet* getSingleton();

	int ScanDevice();
	int ScanMMCSDCard();
	int ScanComDisk(char * filePath = NULL);
	int DetectLostDevice(char * device);
	bool IsHaveDevice(char * device);
	int  ScanDeviceProc();
	static int PUB_THREAD_CALL ScanDeviceThread(void *pParam);
	static int PUB_THREAD_CALL ScanLostDevice(void *pParam);

	int DetectDevice(int diskNO,char * device);
	int ProcLostDevice();

	int DetectFilePath(const char * filePath);
	
	int AddFileIndex(int idx,DISK_INFO & diskInfo,CDiskTable & diskTable);
	int AddComDiskIndex(int idx,DISK_INFO & diskInfo);
	int DeleteFileIndex(const char * device);
	int MarkFormatFlag(const char* device);
	int  ClearAll();
	int  CheckDisk(int diskNO);
	int  GetDiskNum();
	int  GetRecordDiskNum(bool bReCheck = false);
	int  RemoveDisk(char * device,bool bForce = false);
	int  DeleteDiskInfo(const char * device);
	bool IsNeedCheck(time_t curTime);
	bool IsRecordDisk(const char * device);
	bool IsBeInclude(const char * device);
	int  ClearInvalidDir();
	bool IsNeedRescan();
	int  RecomputateDiskCount();
	bool IsLostDiskInsert();
	
	
	CFileIndex * GetIndexFile(char* device);
	void SupportSDcard(bool bSupport)
	{
		m_bSupportSDCard = bSupport;
	}
	void SetCameraNum(int chNum)
	{
		m_cameraNum = chNum;
	}
    int DiskInfoNum()
    {
		int ret = 0;
		m_lock.Lock();
		ret = (int)m_vecDiskInfo.size();
		m_lock.UnLock();
		return ret;
    }
	VEC_DISKINFO* GetDiskInfo()
	{
		return &m_vecDiskInfo;
	}

	CIndexSet * GetIndexSet()
	{
		return & m_idxSet;
	}

	CIndexSet * GetIndexSet_Sub()
	{
		return & m_idxSet_sub;
	}

	char *  GetDevName(int diskNO);
	char *  GetRecordDisk(int diskNO);
	uint64_t GetReduStatus()
	{
		return m_CHRedu;
	}

	int GetChannelNum()
	{
		return (int)m_cameraNum;
	}

	DISK_INFO * GetDiskInfo(int diskNO)
	{
		DISK_INFO* pDisk = NULL;
		m_lock.Lock();
		if(diskNO < (int)m_vecDiskInfo.size())
		{
			pDisk = &m_vecDiskInfo[diskNO];
		}
		m_lock.UnLock();
		return pDisk;
		
	}

	CDisk_Dev * GetDev(int diskNO);
	CDisk_Dev * GetDevByName(const char *dev);
	CDisk_Dev * GetRecordDevice(int diskNO,DISK_INFO & dInfo);

	uint64_t GetAssignFileLBA(const char * device,int fileIndex);
	int CheckDiskStatus(int diskNO);
	static int CallBakDiskActive(int diskNO, int diskNum);
	static int CallBakDiskLost(int diskNO);
	static int CallBakDiskFormat(int diskNO,int progress,int ret);
	int PrintfDiskInfo();
	uint32_t GetRecFileSize()
	{
		return m_recFileSize;
	}
	uint32_t GetDbgLogSize()
	{
		return m_dbgLogSize;
	}
	bool IsLostDisk()
	{
		return m_bLostDisk;
	}
	bool IsDiskError()
	{
		return m_bDiskError;
	}
	void SetScan1stDrvNo(int drvNo)
	{
		m_scanDrvNo = drvNo;
	}
	bool isScanning()
	{
		return m_bScanning;
	}
	static int32_t		BIT_DISK_ACTIVE;			//激动的硬盘
	static int64_t		BIT_DiskLost;				//丢失硬盘
	static int64_t      BIT_DiskErr;				//磁盘错误
	bool         m_bSupportSDCard;					//是否支持SDCard
#ifdef _WIN32
	static int32_t      BIT_NEED_DETECTED;
	char         m_start_drive;
#endif
protected:
	uint16_t	 m_diskNum;			                //有效磁盘数目，包括本地和网络磁盘
	uint16_t	 m_cameraNum;			            //有效视频通道数目

	uint16_t	 m_localDiskCtrlNum;	            //有效本地磁盘接口数目
	bool		 m_bSptRedu;						//是否支持冗余磁盘
	uint32_t	 m_recFileSize;						//录像文件大小
	uint32_t	 m_dbgLogSize;						//调试日志大小
	VEC_DISKINFO m_vecDiskInfo;						//所有的磁盘信息
	LST_DISKINFO m_lstDiskInfo;						//磁盘列表信息

	CIndexSet	 m_idxSet;							//索引集合
	CIndexSet    m_idxSet_sub;                      //子码流相关的磁盘集合

	bool		 m_bLostDisk;						//是否丢失
	bool		 m_bDiskError;						//磁盘出错
	bool         m_bScanning;	                    //正在扫描
	bool         m_bSupportEmmc;                    //是否支持EMMC
	time_t		 m_checkTime;						//
	int			 m_checkWait;						//S
	int          m_scanDrvNo;						//开始扫描的磁盘号
	uint64_t     m_CHRedu;							//冗余通道位数，最多支持64个通道
	vector<string> m_vecLostDevice;                 //丢失的设备
	cross_thread_t m_scanThdID;
	cross_thread_t m_detectLostID;                  //检测并处理丢失硬盘的线程
	CCross_Lock	 m_lock;
	string  m_lost_disk;
};

#endif	//_DISK_INFO_SET_
extern CDiskInfoSet	g_diskInfoSet;
extern CIndexSet   * g_pIndexSet;

#endif // header guard
